package com.thrift.distributed.process;

import com.alibaba.fastjson.JSONObject;
import com.thrift.distributed.service.DistributedService;
import com.thrift.distributed.vo.DistributedLogVo;
import com.thrift.distributed.vo.DistributedResultVo;
import com.thrift.distributed.vo.DistributedVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 服务处理程序
 * Created by 张波 on 2019/9/18.
 */
@Slf4j
@Component
public class DistributedServiceProcess implements DistributedService.Iface {

    @Autowired
    private DistributedComponent distributedComponent;

    /**
     * 服务检查
     * @param distributedVo
     * @return
     * @throws TException
     */
    @Override
    public DistributedResultVo check(DistributedVo distributedVo) throws TException {
        log.debug("请求入参：{}", JSONObject.toJSONString(distributedVo));
        DistributedResultVo distributedResultVo = distributedComponent.check(distributedVo);
        log.debug("返回参数：{}", JSONObject.toJSONString(distributedResultVo));
        return distributedResultVo;
    }

    /**
     * 失败时服务次数退回
     * @param distributedVo
     * @throws TException
     */
    @Override
    public void returnCount(DistributedVo distributedVo) throws TException {
        distributedComponent.returnCount(distributedVo);
    }

    /**
     * 日志收集
     * @param distributedLogVo
     * @throws TException
     */
    @Override
    public void sendLog(DistributedLogVo distributedLogVo) throws TException {
        distributedComponent.sendLog(distributedLogVo);
    }

    /**
     * 服务可用性检测
     * @throws TException
     */
    @Override
    public void ping() throws TException {
        log.info("服务检测");
    }
}
